﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AddingImagesToDatabase
{
    class ImportingImages
    {
        static void Main(string[] args)
        {
            InsertAllImagesInTheDatabase();
        }

        public static void InsertAllImagesInTheDatabase()
        {
            DirectoryInfo dir = new DirectoryInfo("Images");
            BookstoreDataClassesDataContext context = new BookstoreDataClassesDataContext();

            foreach (FileInfo file in dir.GetFiles())
            {
                bool fileAdded = false;
                bool exist = false;
                BookImage currentImage=new BookImage();
                var book =
                    (from b in context.Books
                    where b.ISBN + ".jpg" == file.Name
                    select b).FirstOrDefault();
  
                    var allImages =
                        from i in context.BookImages
                        select i;
                    foreach (var image in allImages)
                    {
                        if (image.Title == file.Name)
                        {
                            exist = true;
                            currentImage = image;
                        }
                    }
                    if (exist == false && book!=null)
                    {
                        BookImage bookImage = new BookImage()
                        {
                            Title = file.Name,
                            Content = GetImage(dir.Name + "/" + file.Name),
                            BookId=book.BookId
                        };
                        context.BookImages.InsertOnSubmit(bookImage);
                        context.SubmitChanges();
                        fileAdded = true;
                    }
                    if (exist == true && book != null)
                    {
                        currentImage.Title = file.Name;
                        currentImage.Content = GetImage(dir.Name + "/" + file.Name);
                        currentImage.BookId = book.BookId;
                        context.SubmitChanges();
                    }
                if (fileAdded == false && exist == false)
                {
                    Console.WriteLine(file.Name + "doesn't match any of the books in the database.");
                }
            }

        }

        static public byte[] GetImage(string location)
        {
           // using (FileStream inFile =
             //   new FileStream(location, FileMode.Open))
          //  {
                byte[] result = File.ReadAllBytes(location);
                return result;
                //byte[] buf = new byte[5 * 1024];
                //List<byte> img = new List<byte>();

                //while (true)
                //{
                //    int bytesRead = inFile.Read(buf, 0, buf.Length);
                //    if (bytesRead == 0)
                //    {
                //        break;
                //    }
                //    for (int i = 0; i < bytesRead; i++)
                //    {
                //        img.Add(buf[i]);
                //    }
                //}
                //byte[] image = img.ToArray();
                //return image;
           // }
        }
    }
}
